package com.yz.jdd.module.main.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yz.jdd.R;
import com.yz.jdd.webview.entity.MsgCountBean;

import java.util.List;


/**
 * 消息列表
 */
public class MsgCountAdapter extends BaseAdapter {

    private Context context;
    private List<MsgCountBean.AppMsgVoListBean> appMsgVoList;

    public MsgCountAdapter(Context context, List<MsgCountBean.AppMsgVoListBean> appMsgVoList) {
        this.context = context;
        this.appMsgVoList = appMsgVoList;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Object getItem(int i) {
        return appMsgVoList.get(i);
    }

    @Override
    public int getCount() {
        return appMsgVoList != null ? appMsgVoList.size() : 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.msg_count_item, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.tv_message_count = (TextView) view.findViewById(R.id.tv_message_count);
            viewHolder.title_tv = (TextView) view.findViewById(R.id.title_tv);
            viewHolder.content_tv = (TextView) view.findViewById(R.id.content_tv);
            viewHolder.imageView = (ImageView) view.findViewById(R.id.image);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.title_tv.setText(appMsgVoList.get(i).getTitle());
        viewHolder.content_tv.setText(appMsgVoList.get(i).getDesc());
        int count = appMsgVoList.get(i).getCount();
        if (count > 0) {
            viewHolder.tv_message_count.setVisibility(View.VISIBLE);
            if (count > 99) {
                viewHolder.tv_message_count.setText("99+");
            } else {
                viewHolder.tv_message_count.setText(count + "");
            }
        } else {
            viewHolder.tv_message_count.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(appMsgVoList.get(i).getAndrIconAddress())) {
            Glide.with(context).load(appMsgVoList.get(i).getAndrIconAddress()).into(viewHolder.imageView);
        }
        return view;
    }

    class ViewHolder {
        ImageView imageView;
        TextView tv_message_count;
        TextView title_tv;
        TextView content_tv;
    }


}
